package SecondQuestion;

import java.util.Scanner;

/**
 * 编程实现控制台版并支持两人对战的五子棋游戏, 1表示黑子，0表示白子，+表示未下子的棋盘
 * @author cyt
 * @date 2020/6/8
 */
public class Gomoku {
    public char[][] board;

    public Gomoku(){
        this.board = new char[16][16];
        for (int i = 0; i <= this.board.length - 1; i++ ){
            for (int j = 0; j <= this.board[i].length - 1 ; j++) {
                this.board[i][j] = '+';
            }
        }
    }
    //将整数转换成需要的char
    public static char valueTransfer(int input){
        if(input >= 0 && input <= 9){
            return (char)('0'+input);
        }else {
            return (char)('a' + input - 10);
        }
    }
    //绘制棋盘
    public void drawBoard(){

        //第一行
        String firstRow = " ";
        for (int i = 0; i <= 15; i++){
            firstRow += " ";
            firstRow += valueTransfer(i);
        }
        System.out.println(firstRow);

        //其余行，i表示行，j表示列
        for (int i = 0; i <= 15; i++){
            //行首
            System.out.print(valueTransfer(i));
            //每一行的+号
            for(int j = 0; j <= 15; j++){
                System.out.print(" "+this.board[i][j]);
            }
            //换行
            System.out.println();
        }
    }

    public void setBoard(int row, int column, char blackOrWhite){
        this.board[row][column] = blackOrWhite;
    }

    //判断这个坐标是否已经有棋子了
    private boolean existPiece(int row, int column){
        return this.board[row][column] != '+';
    }

    //用户输入下子的坐标
    public int[] playOneStep( Scanner sc){
        int[] coordinate = new int[2];
        int row;
        int column;
        while (true){

            System.out.println("横坐标为：");
            row = sc.nextInt();
            System.out.println("纵坐标为：");
            column = sc.nextInt();
            if(!existPiece(row,column)){
                break;
            }else {
                System.out.println("这个位置已经有棋子了，请重新下子！！！");
            }
        }
        coordinate[0] = row;
        coordinate[1] = column;
        return coordinate;
    }

    /**
     * 开始游戏，1表示黑子，0表示白子，+表示没有下子的棋盘
     */
    public void startPlay() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Let's start the game!!!");
        drawBoard();
        int[] coordinator;
        while (true) {
            //黑方下子
            System.out.println("黑方下子，请输入下子的位置：");

                coordinator = playOneStep(sc);
                setBoard(coordinator[0], coordinator[1], '1');
                //打印棋盘
                drawBoard();
                //判断胜负
                if (isVictory(coordinator[0], coordinator[1])) {
                    System.out.println("黑方胜利！！！");
                    break;
                }


            //白方下棋
            System.out.println("白方下子，请输入下子的位置：");

                coordinator = playOneStep(sc);
                setBoard(coordinator[0], coordinator[1], '0');
                //打印棋盘
                drawBoard();
                //判断胜负
                if (isVictory(coordinator[0], coordinator[1])) {
                    System.out.println("白方胜利！！！");
                    break;
                }




        }
        }

    //根据最后一步棋来判断是否有人获得了胜利
    private boolean isVictory(int row, int column){
        char value = this.board[row][column];
        int count;
        //判断横向是否获胜
        count = 1;
        int start = column;
        //往左找一样的棋子
        while (start - 1 >= 0 && this.board[row][start - 1] == value){
            count++;
            start--;
        }
        start = column;
        //往右找一样的棋子
        while (start + 1 <= this.board[row].length - 1 && this.board[row][start + 1] == value){
            count++;
            start++;
        }
        System.out.println(count);
        if(count >= 5){
            return true;
        }

        //判断纵向是否胜利
        count = 1;
        start = row;
        //往上找一样的棋子
        while (start - 1 >= 0 && this.board[start - 1][column] == value){
            count++;
            start--;
        }
        start = column;
        //往下找一样的棋子
        while (start + 1 <= this.board.length - 1 && this.board[start + 1][column] == value){
            count++;
            start++;
        }
        if(count >= 5){
            return true;
        }

        //判断左上到右下的斜线是否胜利
        count = 1;
        int startRow = row;
        int startColumn = column;
        //往左上找一样的棋子
        while (startRow - 1 >= 0 && startColumn - 1 >= 0 && this.board[startRow - 1][startColumn - 1] == value){
            count++;
            startRow--;
            startColumn--;
        }
        startRow = row;
        startColumn = column;
        //往右下找一样的棋子
        while (startRow + 1 <= this.board.length - 1 && startColumn + 1 <= this.board[row].length - 1 && this.board[startRow + 1][startColumn + 1] == value){
            count++;
            startRow++;
            startColumn++;
        }
        if(count >= 5){
            return true;
        }

        //判断左下到右上是否获胜
        count = 1;
        startRow = row;
        startColumn = column;
        //往左下找一样的棋子
        while (startRow + 1 <= this.board.length - 1 && startColumn - 1 >= 0 && this.board[startRow + 1][startColumn - 1] == value){
            count++;
            startRow++;
            startColumn--;
        }
        startRow = row;
        startColumn = column;
        //往右上找一样的棋子
        while (startRow - 1 >= 0 && startColumn + 1 <= this.board.length - 1 && this.board[startRow - 1][startColumn + 1] == value){
            count++;
            startRow--;
            startColumn++;
        }
        if(count >= 5){
            return true;
        }

        return false;
    }

}
